package io.renren.modules.humanResources.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.humanResources.dao.InventoryDao;
import io.renren.modules.humanResources.dto.InboundDTO;
import io.renren.modules.humanResources.dto.InventoryDTO;
import io.renren.modules.humanResources.dto.ProductDTO;
import io.renren.modules.humanResources.entity.InboundEntity;
import io.renren.modules.humanResources.entity.InventoryEntity;
import io.renren.modules.humanResources.service.InventoryService;
import io.renren.modules.humanResources.service.ProductService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 库存信息表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-03-28
 */
@Service
public class InventoryServiceImpl extends CrudServiceImpl<InventoryDao, InventoryEntity, InventoryDTO> implements InventoryService {

    @Autowired
    private ProductService productService;

    @Override
    public QueryWrapper<InventoryEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String productTitle = (String)params.get("productTitle");

        QueryWrapper<InventoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        if (StringUtils.isNotBlank(productTitle)) {
            List<ProductDTO> productDtoList = productService.getByProTitle(productTitle);
            if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(productDtoList)) {
                List<Long> collect = productDtoList.stream().map(ProductDTO::getId).collect(Collectors.toList());
                wrapper.in("product_id", collect);
            } else {
                return null;
            }
        }
        return wrapper;
    }


    @Override
    public InventoryDTO queryByProId(Long productId) {
        QueryWrapper<InventoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        List<InventoryEntity> entityList = this.baseDao.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(entityList)) {
            return ConvertUtils.sourceToTarget(entityList.get(0), InventoryDTO.class);
        }
        return null;
    }

    @Override
    public PageData<InventoryDTO> inventoryPage(Map<String, Object> params) {
        if (getWrapper(params) == null) {
            return new PageData<InventoryDTO>(null, 0);
        }
        IPage<InventoryEntity> page = baseDao.selectPage(
                getPage(params, null, false),
                getWrapper(params)
        );
        PageData<InventoryDTO> pageData = getPageData(page, InventoryDTO.class);
        pageData.getList().forEach(e -> {
            ProductDTO productDTO = productService.get(e.getProductId());
            e.setProductTitle(productDTO == null ? "" : productDTO.getTitle());
        });
        return pageData;
    }

    @Override
    public List<InventoryDTO> inventoryList(Map<String, Object> params) {
        if (getWrapper(params) == null) {
            return null;
        }
        List<InventoryEntity> entityList = this.baseDao.selectList(getWrapper(params));
        if (CollectionUtil.isEmpty(entityList)) {
            return null;
        }
        List<InventoryDTO> dtoList = ConvertUtils.sourceToTarget(entityList, InventoryDTO.class);
        dtoList.forEach(e -> {
            ProductDTO productDTO = productService.get(e.getProductId());
            e.setProductTitle(productDTO == null ? "" : productDTO.getTitle());
            e.setProductDescription(productDTO == null ? "" : productDTO.getDescription());
            e.setProductImgUrl(productDTO == null ? "" : productDTO.getImgUrl());
            e.setProductPrice(productDTO == null ? new BigDecimal("0.00") : productDTO.getPrice());
        });
        return dtoList;
    }
}